package cn.iocoder.yudao.framework.datasource.provider;

import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.support.JdbcUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * TODO
 *
 * @className: JdbcDynamicDataSourceProvider
 * @author: tangkh
 * @date: 2023-09-19 17:01
 */
public class JdbcDynamicDataSourceProvider extends AbstractDataSourceProvider implements DynamicDataSourceProvider {
    private static final Logger log = LoggerFactory.getLogger(JdbcDynamicDataSourceProvider.class);
    private final String url;
    private final String username;
    private final String password;
    private final String driverClassName;
    private String sql;

    public JdbcDynamicDataSourceProvider(String sql, String driverClassName, String url, String username, String password) {
        this.sql = sql;
        this.url = url;
        this.username = username;
        this.password = password;
        this.driverClassName = driverClassName;
    }

    @Override
    public Map<String, DataSource> loadDataSources() {
        Connection conn = null;
        Statement stmt = null;

        try {
            if (!StringUtils.isEmpty(this.driverClassName)) {
                Class.forName(this.driverClassName);
                log.debug("成功加载数据库驱动程序,驱动名:{}", this.driverClassName);
            }

            conn = DriverManager.getConnection(this.url, this.username, this.password);
            stmt = conn.createStatement();
            Map<String, DataSourceProperty> dataSourcePropertiesMap = this.executeStmt(this.sql, stmt);
            Map var4 = this.createDataSourceMap(dataSourcePropertiesMap);
            return var4;
        } catch (Exception var8) {
            log.error(var8.getMessage());
        } finally {
            JdbcUtils.closeConnection(conn);
            JdbcUtils.closeStatement(stmt);
        }

        return null;
    }

    @Override
    public Map<String, DataSourceProperty> dataSourceProperties() {
        Connection conn = null;
        Statement stmt = null;

        try {
            if (!StringUtils.isEmpty(this.driverClassName)) {
                Class.forName(this.driverClassName);
                log.debug("成功加载数据库驱动程序,驱动名:{}", this.driverClassName);
            }

            conn = DriverManager.getConnection(this.url, this.username, this.password);
            stmt = conn.createStatement();
            Map var3 = this.executeStmt(this.sql, stmt);
            return var3;
        } catch (Exception var7) {
            log.error(var7.getMessage());
        } finally {
            JdbcUtils.closeConnection(conn);
            JdbcUtils.closeStatement(stmt);
        }

        return null;
    }

    Map<String, DataSourceProperty> executeStmt(String sql, Statement statement) throws SQLException {
        ResultSet resultSet = statement.executeQuery(sql);
        HashMap dataSourceMap = new HashMap();

        while(resultSet.next()) {
            String url = resultSet.getString(2);
            String username = resultSet.getString(3);
            String password = resultSet.getString(4);
            String alias = resultSet.getString(6);
            DataSourceProperty dataSourceProperty = new DataSourceProperty();
            dataSourceProperty.setUsername(username);
            dataSourceProperty.setPassword(password);
            dataSourceProperty.setUrl(url);
            dataSourceProperty.setPoolName(alias);
            dataSourceMap.put(alias, dataSourceProperty);
        }

        return dataSourceMap;
    }
}

